package us.icitap.timsportal.management;

import us.icitap.timsportal.util.PortalNotification;
import us.icitap.timsportal.web.TimsPortalApp;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PTextField;
import us.icitap.timsportal.web.custom.PortalLabel;
import us.icitap.timsportal.web.custom.PortalVerticalLayout;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Validator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.PasswordField;

@SuppressWarnings("serial")
public class PasswordEditor extends PortalVerticalLayout implements Button.ClickListener, Property.ValueChangeListener {
	
	
	private final PasswordField oldPassword, newPassword, newPassword2;
	private final Label passErrMsg = new Label("<img src=\"./icons/error.gif\" ></img><font color=\"red\">Ju duhet te shkruani te njejtin fjalekalim 2-here, qe ta konfirmoni ate.</font>", Label.CONTENT_XHTML);
	private PTextField usernameTxt;
	private PNativeButton save = new PNativeButton("Ruaj", this);
	private PNativeButton cancel = new PNativeButton("Pastro", this);
	private TimsPortalApp app;
	
	public PasswordEditor(){
		setStyleName("timsportal");
		setWidth("550px");
		setMargin(true);
		
		PortalLabel title = new PortalLabel("Ndrysho Fjalekalimin", Label.CONTENT_XHTML);
		title.setSizeFull();
		title.setStyleName("menu");
		addComponentAsFirst(title);
		
		FormLayout fl = new FormLayout();
		fl.setSpacing(true);		
		usernameTxt = new PTextField("Perdoruesi:");
		oldPassword = new PasswordField("Fjalekalimi i vjeter:");
		oldPassword.setImmediate(true);
		oldPassword.setRequired(true);
		oldPassword.addListener((Property.ValueChangeListener)this);
		oldPassword.addValidator(new Validator(){
			boolean authenticated = false;

			@Override
			public void validate(Object value) throws InvalidValueException {
				if(value!=null && (value instanceof String)){
					if (!(authenticated = app.getAdConnection().authenticate(usernameTxt.getValue().toString(), value.toString()))){
						throw new InvalidValueException("Fjalekalimi i vjeter nuk eshte i sakte!");
					}	
				}
			}

			@Override
			public boolean isValid(Object value) {
				System.out.println("authenticated "+authenticated);
				if(value==null || !(value instanceof String))
					return false;
				return authenticated;
			}
			
		});
		newPassword = new PasswordField("Fjalekalimi i ri:");
		newPassword.setImmediate(true);
		newPassword.setRequired(true);
		newPassword2 = new PasswordField("Konfirmo fjalekalimin e ri:");
		newPassword2.setImmediate(true);
		newPassword2.setRequired(true);
//		passErrMsg.setIcon(new ThemeResource("./icons/error.gif"));
		passErrMsg.setVisible(false);
		
		fl.addComponent(usernameTxt);
		fl.addComponent(oldPassword);
		fl.addComponent(newPassword);
		fl.addComponent(newPassword2);
		
		HorizontalLayout footer = new HorizontalLayout();
		footer.setSizeFull();
		footer.setMargin(false);
		footer.setStyleName("wizard");
		footer.addComponent(cancel);
		footer.addComponent(save);
		footer.setComponentAlignment(save, Alignment.MIDDLE_RIGHT);
		footer.setWidth("100%");
		
		addComponent(fl);
		addComponent(passErrMsg);
		addComponent(footer);		
		addComponent(new Label("*Fjalekalimi i ri duhet te jete 8 karaktere ose me shume.", Label.CONTENT_XHTML));
		addComponent(new Label("*Fjalekalimi i ri nuk duhet te permbaje emrin.", Label.CONTENT_XHTML));
		addComponent(new Label("*Fjalekalimi i ri duhet te jete i ndryshem nga 3 fjalekalimet e fundit.", Label.CONTENT_XHTML));
	}
	
	@Override
	public void attach() {
		// TODO Auto-generated method stub
		super.attach();
		usernameTxt.setValue(""+getApplication().getSessionHandler().getUsername().toUpperCase());
		usernameTxt.setReadOnly(true);
		this.app = getApplication();
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if(save==event.getButton()){
			passErrMsg.setVisible(false);

			if(!oldPassword.isValid()){
				oldPassword.focus();
				return;
			}
			
			if(!newPassword.isValid() || !newPassword2.isValid()){
				PortalNotification.showWarning("Fjalekalimi i ri nuk mund te jete bosh.", this.getWindow());
				newPassword.focus();
				return;
			}
			String un = (String) usernameTxt.getValue();
			String oldpass = (String) oldPassword.getValue();
			String newpass = (String) newPassword.getValue();
			String newpass2 = (String) newPassword2.getValue();
			if(!newpass.equals(newpass2)){
				passErrMsg.setVisible(true);
				newPassword2.focus();
				return;
			}
			
			boolean result = app.getAdConnection().updatePassword(un, oldpass, newpass);
			if(result){
				PortalNotification.showTrayMessage("Fjalekalimi u ndryshua me sukses.", this.getWindow());
				clear();
			}else{
				PortalNotification.showWarning("Fjalekalimi nuk mund te ndryshohet.", "<BR/> Fjalekalimi i ri duhet te plotesoje 3 kushtet e poshteshenuar ne forme.", this.getWindow(), true);
			}
		} else if(cancel==event.getButton()){
			clear();
		}
	}
	
	private void clear(){
		oldPassword.setValue("");
		newPassword.setValue("");
		newPassword2.setValue("");
		passErrMsg.setVisible(false);
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		// TODO Auto-generated method stub
		if(event.getProperty()==oldPassword){
			oldPassword.commit();
		}
	}

}
